package testview.zhen.com.myapplication.hencoder.practice_draw1

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Path
import android.graphics.RectF
import android.util.AttributeSet
import android.view.View

class Practice11PieChartView : View {





    constructor(context: Context) : super(context) {}

    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {



    }

    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {}

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)

        var paint: Paint= Paint()
        var paintLine: Paint= Paint()

        paint.strokeWidth = 3f
        paint.style = Paint.Style.FILL

        paintLine.strokeWidth = 1f
        paintLine.style = Paint.Style.STROKE
        paintLine.color = Color.WHITE

        canvas.drawColor(Color.GREEN)
        val widthCenter = canvas.width / 2 //圆心x
        val heightCenter = canvas.height / 2 //圆心y

        val radius = heightCenter / 2

        val rectF = RectF((widthCenter - radius).toFloat(),
                (heightCenter - radius).toFloat(), (widthCenter + radius).toFloat(), (heightCenter + radius).toFloat())

        val rectFRed = RectF((widthCenter - radius - 10).toFloat(),
                (heightCenter - radius - 10).toFloat(), (widthCenter + radius - 10).toFloat(), (heightCenter + radius - 10).toFloat())


        val path = Path()
        path.moveTo(widthCenter - 0.66f * radius, heightCenter + 0.66f * radius)
        path.lineTo(widthCenter - 0.8f * radius, heightCenter + 0.8f * radius)
        path.lineTo(widthCenter.toFloat() - 0.8f * radius - 60f, heightCenter + 0.8f * radius)
        canvas.drawPath(path, paintLine)


        //先画线后画图原因是线的位置不准，会被饼图遮盖（后画会覆盖）
        paintLine.strokeWidth = 1f
        paintLine.textSize = 15f
        paintLine.style = Paint.Style.FILL
        canvas.drawText("KitKat", widthCenter.toFloat() - 0.8f * radius - 130f, heightCenter.toFloat() + 0.8f * radius + 5f, paintLine)

        //红色
        paint.color = Color.parseColor("#EE2B29")
        canvas.drawArc(rectFRed, 180f, 120f, true, paint)
        //蓝色
        paint.color = Color.parseColor("#1E80F0")
        canvas.drawArc(rectF, 75f, 105f, true, paint)
        //绿色
        paint.color = Color.parseColor("#118575")
        canvas.drawArc(rectF, 20f, 50f, true, paint)
        //灰色
        paint.color = Color.parseColor("#8c8c8c")
        canvas.drawArc(rectF, 10f, 8f, true, paint)
        //紫色
        paint.color = Color.parseColor("#830A9B")
        canvas.drawArc(rectF, 0f, 8f, true, paint)
        //背景色
        paint.color = Color.parseColor("#506E7A")
        canvas.drawArc(rectF, 355f, 5f, true, paint)
        //黄色
        paint.color = Color.parseColor("#FDB60D")
        canvas.drawArc(rectF, 300f, 55f, true, paint)


        //        综合练习
        //        练习内容：使用各种 Canvas.drawXXX() 方法画饼图
    }
}
